package cn.wolfcode.p2p.business.service.impl;

import java.math.BigDecimal;

import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleMapper;
import cn.wolfcode.p2p.business.query.PaymentScheduleDetailQueryObject;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PaymentScheduleServiceImpl implements IPaymentScheduleService {

    @Autowired
    private PaymentScheduleMapper paymentScheduleMapper;
    @Autowired
    private IPaymentScheduleDetailService paymentScheduleDetailService;

    @Override
    public void createPaymentsSchedule(BidRequest br) {

        //总期数
        Integer monthIndexs = br.getReturnMonthes();
        //发布时间为还款时间  基准时间
        Date beginDate = br.getPublishTime();
        for (int i = 0; i < monthIndexs; i++) {
            PaymentSchedule ps = new PaymentSchedule();
            ps.setBidRequestType(br.getBidRequestType());
            ps.setReturnType(br.getReturnType());
            ps.setBidRequestId(br.getId());
            ps.setBorrowUser(br.getCreateUser());
            ps.setBidRequestTitle(br.getTitle());

            //当前期还款截止时间
            int monthIndex = i + 1;
            ps.setDeadLine(DateUtils.addMonths(beginDate, monthIndex));
            ps.setMonthIndex(monthIndex);
            //状态设置为还款中
            ps.setState(Constants.PAYMENT_STATE_NORMAL);

            //当前期还款总额
            BigDecimal totalAmount = CalculatetUtil.calMonthToReturnMoney
                    (ps.getReturnType(), br.getBidRequestAmount(), br.getCurrentRate(), monthIndex, br.getReturnMonthes());
            ps.setTotalAmount(totalAmount);

            //当前期还款利息
            BigDecimal monthlyInterest = CalculatetUtil.calMonthlyInterest
                    (ps.getReturnType(), br.getBidRequestAmount(), br.getCurrentRate(), monthIndex, br.getReturnMonthes());
            ps.setInterest(monthlyInterest);

            //当前期还款本金
            ps.setPrincipal(ps.getTotalAmount().subtract(ps.getInterest()));

            paymentScheduleMapper.insert(ps);

            //创建当前还款计划期的收款计划
            paymentScheduleDetailService.createPaymentDetail(ps, br);
        }
    }

    @Override
    public PageInfo listForPage(PaymentScheduleDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List list = paymentScheduleMapper.selectForPage(qo);
        return new PageInfo(list);

    }

    @Override
    public PaymentSchedule getById(Long id) {
        return paymentScheduleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(PaymentSchedule ps) {

        Assert.isFalse(0 == paymentScheduleMapper.updateByPrimaryKey(ps), "付款计划更新操作异常,[乐观锁失败]");
    }

    @Override
    public int getCountByStateAndBidRequest(Long bidRequestId, int state) {
        return paymentScheduleMapper.getCountByStateAndBidRequest(bidRequestId, state);
    }

    @Override
    public void overdueAmountCheck() {

        //查询出有滞纳状态的数据
        List<PaymentSchedule> ps = paymentScheduleMapper.getByOverDueState(Constants.BIDREQUEST_STATE_PAY_BACK_OVERDUE);

        for (PaymentSchedule p : ps) {

            //滞纳金公式=(当前还款期总额+已有滞纳金)*0.8%
            BigDecimal amount = (p.getTotalAmount().add(p.getOverdueAmount())).multiply(new BigDecimal(0.008));
            p.setOverdueAmount(p.getOverdueAmount().add(amount));

            //设置滞纳金的金额
            paymentScheduleMapper.updateByPrimaryKey(p);


        }
    }
}



